package com.cyt.work.enrollment.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.enrollment.dao.EnrollmentHomeinfoDao;
import com.cyt.work.enrollment.model.po.EnrollmentHomeinfo;
import com.cyt.work.enrollment.model.vo.EnrollmentHomeinfoVo;
import com.cyt.work.enrollment.service.EnrollmentHomeinfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 自主招生-家庭信息Service层实现类
 *
 * @author caideyong
 * @date 2019-11-22 14:35:39
 */
@Service("enrollmentHomeinfoService")
public class EnrollmentHomeinfoServiceImpl extends BaseServiceImpl<EnrollmentHomeinfo> implements EnrollmentHomeinfoService {

    @Resource
    private EnrollmentHomeinfoDao enrollmentHomeinfoDao;

    @Autowired
    public void setBaseDao(EnrollmentHomeinfoDao enrollmentHomeinfoDao) {
        this.baseDao = enrollmentHomeinfoDao;
    }

    /**
     * 查询对象集合
     *
     * @param enrollmentHomeinfoVo 对象
     * @return List 对象集合
     * @author caideyong
     * @date 2019-11-22 14:35:39
     */
    @Override
    public List<EnrollmentHomeinfo> listExample(EnrollmentHomeinfoVo enrollmentHomeinfoVo) {
        Example example = new Example(EnrollmentHomeinfo.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if(StringUtils.isNotBlank(enrollmentHomeinfoVo.getStuinfoId())){
            criteria.andEqualTo("stuinfoId", enrollmentHomeinfoVo.getStuinfoId());
        }
        return enrollmentHomeinfoDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param enrollmentHomeinfoVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 14:35:39
     */
    @Override
    public ReturnResult page(EnrollmentHomeinfoVo enrollmentHomeinfoVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<EnrollmentHomeinfo> list = listExample(enrollmentHomeinfoVo);
        PageInfo<EnrollmentHomeinfo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param enrollmentHomeinfoVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 14:35:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(EnrollmentHomeinfoVo enrollmentHomeinfoVo, SysUser sysUser) {
        //判断主键id是否存在,如果存在就更新,不存在就新建
        if (StringUtils.isBlank(enrollmentHomeinfoVo.getId())) {
            EnrollmentHomeinfo enrollmentHomeinfo = new EnrollmentHomeinfo(sysUser);
            enrollmentHomeinfo.setName(enrollmentHomeinfoVo.getName());
            enrollmentHomeinfo.setTitle(enrollmentHomeinfoVo.getTitle());
            enrollmentHomeinfo.setWorkPlace(enrollmentHomeinfoVo.getWorkPlace());
            enrollmentHomeinfo.setPosition(enrollmentHomeinfoVo.getPosition());
            enrollmentHomeinfo.setPhone(enrollmentHomeinfoVo.getPhone());
            enrollmentHomeinfo.setStuinfoId(enrollmentHomeinfoVo.getStuinfoId());
            enrollmentHomeinfo.setCreator(enrollmentHomeinfoVo.getCreator());
            enrollmentHomeinfo.setUpdator(enrollmentHomeinfoVo.getUpdator());
            enrollmentHomeinfoDao.insert(enrollmentHomeinfo);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            EnrollmentHomeinfo enrollmentHomeinfo = enrollmentHomeinfoDao.selectByPrimaryKey(enrollmentHomeinfoVo.getId());
            enrollmentHomeinfo.setName(enrollmentHomeinfoVo.getName());
            enrollmentHomeinfo.setTitle(enrollmentHomeinfoVo.getTitle());
            enrollmentHomeinfo.setWorkPlace(enrollmentHomeinfoVo.getWorkPlace());
            enrollmentHomeinfo.setPosition(enrollmentHomeinfoVo.getPosition());
            enrollmentHomeinfo.setPhone(enrollmentHomeinfoVo.getPhone());
            enrollmentHomeinfo.setStuinfoId(enrollmentHomeinfoVo.getStuinfoId());
            enrollmentHomeinfo.setCreator(enrollmentHomeinfoVo.getCreator());
            enrollmentHomeinfo.setUpdator(enrollmentHomeinfoVo.getUpdator());
            enrollmentHomeinfo.setUpdateTime(new Date());
            enrollmentHomeinfo.setUpdator(sysUser.getId());
            enrollmentHomeinfoDao.updateByPrimaryKey(enrollmentHomeinfo);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param enrollmentHomeinfoVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 14:35:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(EnrollmentHomeinfoVo enrollmentHomeinfoVo, SysUser sysUser) {
        //要修改的字段
        EnrollmentHomeinfo enrollmentHomeinfo = new EnrollmentHomeinfo();
        enrollmentHomeinfo.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        enrollmentHomeinfo.setUpdator(sysUser.getId());
        enrollmentHomeinfo.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(EnrollmentHomeinfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(enrollmentHomeinfoVo.getId())) {
            criteria.andIn("id", Arrays.asList(enrollmentHomeinfoVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        enrollmentHomeinfoDao.updateByExampleSelective(enrollmentHomeinfo, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param enrollmentHomeinfoVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 14:35:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(EnrollmentHomeinfoVo enrollmentHomeinfoVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(EnrollmentHomeinfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(enrollmentHomeinfoVo.getId())) {
            criteria.andIn("id", Arrays.asList(enrollmentHomeinfoVo.getId().split(",")));
        }
        enrollmentHomeinfoDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    @Override
    public void insertList(List<EnrollmentHomeinfo> enrollmentHomeinfoList) {
        enrollmentHomeinfoDao.insertList(enrollmentHomeinfoList);
    }
}